﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Web.Security;
using System.Web.UI.WebControls;
using SalesAndStoreApp.BLL;
using SalesAndStoreApp.DAL.DAO;

namespace SalesAndStoreApp.UI.SalesManager
{
    public partial class SaleProductUI : System.Web.UI.Page
    {
        private ProductManager productManager = new ProductManager();
        private VendorManager vendorManager = new VendorManager();
        private double totalPrice;
        private PurchaseManager _purchaseManager = new PurchaseManager();
        private SalesSystem salesManager = new SalesSystem();
        private InventoryManager inventoryManager = new InventoryManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            if (!IsPostBack)
            {
                messageLabel.Text = "";
                productDropDownList.DataSource = productManager.GetAll();
                productDropDownList.DataTextField = "Name";
                productDropDownList.DataValueField = "Id";
                productDropDownList.DataBind();
                if (productDropDownList.Items.Count != 0)
                {
                    Product product = productManager.GetById(int.Parse(productDropDownList.Items[0].Value));
                    unitPriceLabel.Text = product.SaleUnitPrice.ToString();
                }
                Session["products"] = new List<Product>();
                dateTextBox.Text = DateTime.Now.ToShortDateString();
                int defaultQuantity = 1;
                quantityTextBox.Text = defaultQuantity.ToString();
                totalPriceLabel.Text = new int().ToString();
            }
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            Product product = productManager.GetById(int.Parse(productDropDownList.SelectedValue));
            List<Product> products = GetProductsFromProductView();
            product.Inventory.Quantity = Convert.ToInt32(quantityTextBox.Text);
            Session["products"] = productManager.AddProductTo(product, products);
            productStoreListView.DataBind();
            foreach (var product1 in products)
            {
                totalPrice += product1.SaleUnitPrice * product1.Inventory.Quantity;
            }
            totalPriceLabel.Text = totalPrice.ToString(CultureInfo.InvariantCulture);
        }

        private List<Product> GetProductsFromProductView()
        {
            var products = new List<Product>();
            foreach (var item in productStoreListView.Items)
            {
                Label productIdLabel = (Label)item.FindControl("IdLabel");
                Label productNameLabel = (Label)item.FindControl("NameLabel");
                Label productUnitPriceLabel = (Label)item.FindControl("UnitPriceLabel");
                Label productQuantity = (Label)item.FindControl("QuantityLabel");
                Product product = new Product();
                product.Id = int.Parse(productIdLabel.Text);
                product.Name = productNameLabel.Text;
                product.SaleUnitPrice = Convert.ToDouble(productUnitPriceLabel.Text);
                product.Inventory = inventoryManager.GetInventoryOfProduct(product.Id);
                product.Inventory.Quantity = Convert.ToInt32(productQuantity.Text);
                products.Add(product);
            }
            return products;
        }

        protected void saleButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProductCarriage productCarriage = new ProductCarriage();
                productCarriage.Products = GetProductsFromProductView();
                productCarriage.Date = Convert.ToDateTime(dateTextBox.Text).Date;
                productCarriage.TotalPrice = Convert.ToDouble(totalPriceLabel.Text);
                if (salesManager.Save(productCarriage))
                {
                    messageLabel.ForeColor = Color.Green;
                    messageLabel.Text = "Saved Successfully!";
                }
            }
            catch (Exception exception)
            {

                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }
        }

        protected void logOutLink_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Cache.SetNoStore();
            FormsAuthentication.RedirectToLoginPage();
        }

        protected void productDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Product product = productManager.GetById(int.Parse(productDropDownList.SelectedValue));
            unitPriceLabel.Text = product.SaleUnitPrice.ToString();
        }
    }
}